<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addconmy(conpmy)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleCommand">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{ type: 'add',data:data }">添加子部门</el-dropdown-item>
                        <el-dropdown-item :command="{ type: 'edit', data:data}">编辑部门</el-dropdown-item>
                        <el-dropdown-item :command="{ type: 'del', id: data.id }">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <AddDept ref="AddDate" :show-dialog.sync="showDialog" :notdata="notData" :listcode="listcode" @close-dialog="handleClose" />
  </div>
</template>

<script>

import AddDept from './components/add-dept' // 引入新增部门组件
import { DeactivatedAPI, Deldetlet } from '@/api/departments'
import { truesListData } from '@/utils/index'
export default {
  name: 'Dashboard',
  components: {
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: true,
      notData: {},
      listcode: {},
      conpmy: {}
    }
  },
  created() {
    this.DeactivatedList()
    this.showDialog = false
  },
  methods: {
    /**
     * 渲染部门跟完成树形结构操作
     */
    async DeactivatedList() {
      const { data: res } = await DeactivatedAPI()
      // console.log(res)
      this.departs = truesListData(res.depts, '')
      this.showDialog = false
      this.listcode = res.depts
      console.log(this.listcode)
      this.conpmy.id = ''
      this.conpmy.children = this.departs
    },
    /**
     * 删除操作
     */
    handleCommand({ type, id, data }) {
      if (type === 'add') {
        console.log('添加操作')
        this.showDialog = true
        this.notData = data
      }
      if (type === 'edit') {
        console.log('编辑操作')
        this.editDepts(data)
      }
      if (type === 'del') {
        console.log('删除操作')
        this.$confirm('!确认要进行删除吗', '提示').then(async() => {
          await Deldetlet(id)
          // 添加提示
          this.$message.success('删除操作成功')
          /* 删除成功，重新获取数据*/
          this.DeactivatedList()
        })
      }
    },
    /**
     * 添加方法，关闭弹窗
     */
    handleClose() {
      this.showDialog = false
    },
    addconmy(data) {
      // console.log(data)
      this.showDialog = true
      this.notData = data
    },
    /**
     * 点击编辑部门
     */
    editDepts(data) {
      // console.log(data)
      this.showDialog = true
      this.notData = data
      console.log(this.notData = data)
      this.$nextTick(() => {
        this.$refs.AddDate.bianji()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
